<template>
  <fileManageView
    :tableData="tableData"
    :total="total"
    :pageSize="pageSize"
    :currentPage="currentPage"
    :clients="clients"
    @changePage="changePage"
    @sizeChange="sizeChange"
    @downLoad="downLoad"
    @delFile="delFile"
    @findFile="findFile"
    @checkBoxChange="checkBoxChange"
    @batchDeletion="batchDeletion"
  ></fileManageView>
</template>

<script>
  import fileManageView from './fileManageView';

  export default {
    components: {
      fileManageView
    },
    name: "fileManageLogic",
    props: {},
    filters: {},
    computed: {},
    watch: {},
    data() {
      return {
        tableData: [], // 文件列表
        total: 0, // 总数
        pageSize: 10, // 每页条数
        currentPage: 1, // 当前页
        clients: [], // 应用列表
        selectCheckBox: [], // 存储多选的
        host: `http://base.kingborn.org:27002`,
        fileSeach: "",
        clientsValue: ""
      }
    },
    created() {
      this.getClientSList();
    },
    mounted() {

    },
    methods: {
      /**
       * 获取文件列表
       * @param obj
       *
       * obj = {
        UserId: "123456",
        currentPage: this.currentPage - 1, // 当前页
        pageSize: "10" // 一页显示多少个
      }
       */
      getFileList(obj) {
        _https.post.getFile(obj).then(res => {
          let content = res.data.content.map((item, index) => {
            item.index = (index + 1) + res.data.pageable.pageNumber * this.pageSize + "";
            return item;
          })
          let newContent = [...content];
          let client = this.clients;
          let newArr = newContent.map((item) => {
            for (let i = 0; i < client.length; i++) {
              if (item.clienId == client[i].clientId) {
                item.clienId = client[i].name;
                break;
              }
            }
            return item;
          })
          this.tableData = newArr;
          this.total = res.data.totalElements;
        })
      },
      // 获取应用列表
      getClientSList() {
        this.isLoading = true;
        _https.post.getAppList().then(res => {
          if (res.data) {
            let newArr = [...res.data];
            newArr.unshift({
              clientId: "",
              name: "全部"
            });
            this.clients = newArr;
          }
        }).then(() => {
          let obj = {
            UserId: "123456",
            currentPage: this.currentPage - 1, // 当前页
            pageSize: this.pageSize // 一页显示多少个
          }
          this.getFileList(obj);
        })
      },
      /**
       * 改变当前页面的时候
       * @param page 当前页
       */
      changePage(page) {
        this.currentPage = page;
        let obj = {
          UserId: "123456",
          fname: this.fileSeach,
          clientId: this.clientsValue,
          currentPage: page - 1, // 当前页
          pageSize: this.pageSize // 一页显示多少个
        }
        this.getFileList(obj);
      },
      /**
       * 修改每页条数
       * @param num 条数
       */
      sizeChange(num) {
        this.pageSize = num;
        let obj = {
          UserId: "123456",
          fname: this.fileSeach,
          clientId: this.clientsValue,
          currentPage: this.currentPage - 1, // 当前页
          pageSize: this.pageSize // 一页显示多少个
        }
        this.getFileList(obj);
      },
      /**
       * 根据应用id和文件名查询文件列表
       * @param fileSeach
       */
      findFile(fileSeach, clientsValue) {
        this.clientsValue = clientsValue;
        this.fileSeach = fileSeach;
        this.currentPage = 1;
        let obj = {
          UserId: "123456",
          currentPage: 0, // 当前页
          pageSize: this.pageSize, // 一页显示多少个
          fname: fileSeach,
          clientId: clientsValue
        }
        this.getFileList(obj);

      },
      /**
       * 下载文件
       * @param index 当前行下标
       * @param row 当前行
       */
      downLoad(index, row) {
        window.open(this.host + row.abpath);
      },
      /**
       * 删除文件
       * @param index 当前行下标
       * @param row 当前行
       */
      delFile(index, row) {
        let newObj = {
          fid: row.fid
        }
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _https.post.getDeleteFile(newObj).then(res => {
            if (res.status == 200) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              let obj = {
                UserId: "123456",
                currentPage: this.currentPage - 1, // 当前页
                pageSize: this.pageSize // 一页显示多少个
              }

              _https.post.getFile(obj).then(res => {
                this.total = res.data.totalElements;
              }).then(() => {
                let currentPage = this.currentPage;
                if (this.total % this.pageSize == 0 && currentPage != 1) {
                  currentPage = Math.ceil(this.total / this.pageSize);
                  this.currentPage = currentPage;
                }
                let newObj = {
                  UserId: "123456",
                  currentPage: this.currentPage - 1, // 当前页
                  pageSize: this.pageSize // 一页显示多少个
                }
                this.getFileList(newObj);
              })
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      checkBoxChange(selection) {
        this.selectCheckBox = selection;
      },
      // 批量删除
      batchDeletion() {
        let selectCheckBox = this.selectCheckBox;
        if (selectCheckBox.length == 0) {
          this.$message({
            type: 'warning',
            message: '请选择要删除的文件!'
          });
          return;
        }
        let newArr = selectCheckBox.map(item => {
          return item.fid;
        });

        let newObj = {
          fid: newArr.join(",")
        }
        this.$confirm('此操作将永久删除这些文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _https.post.getDeleteFile(newObj).then(res => {
            console.log("res123", res);
            if (res.status == 200) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              let obj = {
                UserId: "123456",
                currentPage: this.currentPage - 1, // 当前页
                pageSize: this.pageSize // 一页显示多少个
              }

              _https.post.getFile(obj).then(res => {
                this.total = res.totalElements;
              }).then(() => {
                let currentPage = this.currentPage;
                if (this.total % this.pageSize == 0 && currentPage != 1) {
                  currentPage = Math.ceil(this.total / this.pageSize);
                  this.currentPage = currentPage;
                }
                let newObj = {
                  UserId: "123456",
                  currentPage: this.currentPage - 1, // 当前页
                  pageSize: this.pageSize // 一页显示多少个
                }
                this.getFileList(newObj);
              })
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    },
    destroyed() {

    }
  }

</script>

<style scoped>

</style>
